package fr.mywiki.business.entry.meta;

import java.util.HashSet;
import java.util.Set;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class EntryModelTest extends TestCase {
  private static final String IDEA_MODEL = "idea";
  
  public EntryModelTest(String a_name) {
    super(a_name);
  }

  public static Test suite() {
    TestSuite suite = new TestSuite();
    suite.addTestSuite(EntryModelTest.class);
    return suite;
  }
  
  /** Try to create a new model : idea */
  public void testCreateNewModel1() {
    EntryModel model = new EntryModel(IDEA_MODEL);
    assertEquals(IDEA_MODEL, model.getName());
    
    // Add the name field
    try {
      model.addProperty(PropertyFactory.createSimpleTextProperty("name",true));
      // Add the problem-solution-results fields
      model.addProperty(PropertyFactory.createWikiTextProperty("problem",true));
      model.addProperty(PropertyFactory.createWikiTextProperty("solution",true));
      model.addProperty(PropertyFactory.createWikiTextField("results"));
      
      // Add the nature link field
      Set potentialNatures = new HashSet();
      potentialNatures.add("nature1");
      potentialNatures.add("nature2");
      potentialNatures.add("nature3");
      model.addProperty(PropertyFactory.createChoiceProeprty("nature", 1, 1,
          potentialNatures, true));

      // Add the themes links field
      Set potentialThemes = new HashSet();
      potentialThemes.add("theme1");
      potentialThemes.add("theme1");
      potentialThemes.add("theme3");
      model.addProperty(PropertyFactory
          .createChoiceField("theme", potentialThemes));

      // Add links to other ideas
      model.addProperty(PropertyFactory.createChoiceField("related"));
      
    } catch (ModelException e) {
      fail("Exception thrown");
    }
    
    Set fields = model.getFields();
    assertEquals(7,fields.size());
  }
  
  public void testCreateNewModel2() {
    EntryModel model = new EntryModel(IDEA_MODEL);
    assertEquals(IDEA_MODEL, model.getName());
    
    // Add twice a field named "name"
    try {
      model.addProperty(PropertyFactory.createSimpleTextProperty("name"));
    } catch (ModelException e) {
      fail("Exception thrown in creation");
    }
    try {
      model.addProperty(PropertyFactory.createWikiTextField("name"));
      fail("Managed to add two fields with the same name");
    } catch (ModelException e) {
    }
  }
}
